package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@ApiOperation("套餐相关操作")
@Slf4j
@RequestMapping("/admin/setmeal")
public class setmealController {
    @Autowired
    private SetmealService setmealService;
    @PostMapping
    @CacheEvict(cacheNames = "setmealCach", key="#setmealDTO.categoryId")
    public Result add(@RequestBody SetmealDTO setmealDTO) {
        setmealService.add(setmealDTO);
        return Result.success("新增套餐成功");
    }

//    @GetMapping("page")
//    public Result<PageResult> page( SetmealPageQueryDTO setmealPageQueryDTO) {
//       PageResult pageResult= setmealService.page(setmealPageQueryDTO);
//       return Result.success(pageResult);
//    }


    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult=setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }






    @PostMapping("/status/{status}")
    @ApiOperation("启售停售状态更改")
    @CacheEvict(cacheNames = "setmealCach", allEntries = true)
    public Result<String> updateStatus(@PathVariable Integer status,Long id){
        setmealService.updateStatus(status,id);
        return Result.success("状态修改成功");
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> selectById(@PathVariable Long id){
   SetmealVO setmealVO= setmealService.selectById(id);
        return Result.success(setmealVO);
    }
    @PutMapping
    @ApiOperation("根据id修改套餐")
    @CacheEvict(cacheNames = "setmealCach", allEntries = true)
    public Result<String> update(@RequestBody SetmealDTO setmealDTO){
    setmealService.update(setmealDTO);
        return Result.success("修改成功");
    }
    @DeleteMapping
    @ApiOperation("批量删除")
    @CacheEvict(cacheNames = "setmealCach", allEntries = true)
    public Result<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteIds(ids);
        return Result.success("批量删除成功");
    }
}
